// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.migration;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dengues.core.migration.AbstractMigrationService;
import org.dengues.core.migration.MigrationExecResult;
import org.dengues.core.resource.DenguesProjectFactory;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.model.DenguesModelManager;
import org.dengues.model.project.ProjectType;
import org.dengues.model.project.VersionMigration;
import org.eclipse.emf.common.util.EList;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-5-8 qiang.zhang $
 * 
 */
public class MigrationAlertStartUp implements IStartup {

    private static Logger logger = Logger.getLogger(MigrationAlertStartUp.class);

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IStartup#earlyStartup()
     */
    public void earlyStartup() {
        final IWorkbench workbench = PlatformUI.getWorkbench();
        ProjectType projectType = DenguesProjectFactory.getDenguesProject().getProjectType();
        List<String> mids = new ArrayList<String>();
        EList<VersionMigration> ms = projectType.getMigrations();
        for (VersionMigration vm : ms) {
            mids.add(vm.getId());
        }
        final List<AbstractMigrationService> execMS = new ArrayList<AbstractMigrationService>();
        List<AbstractMigrationService> ams = AbstractMigrationService.getAllMigrations();
        boolean isM = false;
        for (AbstractMigrationService abser : ams) {
            if (!mids.contains(abser.getId())) {
                MigrationExecResult em = abser.executeMigration();
                VersionMigration cvm = DenguesModelManager.getProjectFactory().createVersionMigration();
                cvm.setId(abser.getId());
                cvm.setVersion(projectType.getVersion());
                cvm.setStatus(em.getId());
                projectType.getMigrations().add(cvm);
                isM = true;
                switch (em) {
                case SUCCESS_ALERT:
                    execMS.add(abser);
                    break;
                case FAILURE_NOT_ALERT:
                    logger.error("fail to execute Migration Task: \n" + abser.getName() + "(" + abser.getId() + ")");
                    break;
                case SUCCESS_NOT_ALERT:
                    logger.info("success to execute Migration Task: \n" + abser.getName() + "(" + abser.getId() + ")");
                    break;
                }
            }
        }
        if (isM) {
            WarehouseResourceFactory.save(projectType);
            workbench.getDisplay().asyncExec(new Runnable() {

                public void run() {
                    IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
                    if (window != null) {
                        if (!execMS.isEmpty()) {
                            MigrationSummaryDialog dialog = new MigrationSummaryDialog(window.getShell(), execMS);
                            dialog.open();
                        }
                    }
                }
            });
        }
    }
}
